package com.ruoyi.web.controller.initData;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.dao.SysRoleDao;
import com.ruoyi.system.domain.dao.SysUserDao;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
public class SysUserRoleInit {

    @Autowired
    private OaMapper oaMapper;
    @Autowired
    private SysUserDaoMapper sysUserDaoMapper;
    @Autowired
    private SysDeptDaoMapper sysDeptDaoMapper;
    @Autowired
    private SysPostDaoMapper sysPostDaoMapper;
    @Autowired
    private SysUserPostMapper sysUserPostMapper;
    @Autowired
    private SysRoleDaoMapper sysRoleDaoMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 初始化 用户角色，绑定角色到具体用户
     */
    public void bindRole2User() {
        List<SysUserDao> list = sysUserDaoMapper.selectList(new QueryWrapper<>());
        if (list != null && list.size() > 0) {
            list.stream().forEach(item -> {
                // userUuid=b96e1852522a46399b914fcd8e798d2c,empId=15d41a949a5e487ca6e9ff37f2f60f1e
                if (StringUtils.isEmpty(item.getOldData())) {
                    return;
                }
                String[] split = item.getOldData().split(",");
                if (split.length > 1) {
                    String userUuid = split[0].split("=")[1];
                    String empId = split[1].split("=")[1];
                    List<Map<String, String>> listData = oaMapper.listDataByCondition("rel_t_user_role", "sys_t_user_uuid", userUuid);
                    if (listData != null && listData.size() > 0) {
                        Map<String, String> val = listData.get(0);

                        String roleUuid = val.get("sys_t_role_uuid"); // 旧数据所在部门
                        //roleUuid=ac00a27521ba4ed5ad9bc3bfa1293a3e

                        if (StringUtils.isNotEmpty(roleUuid)) {
                            LambdaQueryWrapper lw = new LambdaQueryWrapper<SysRoleDao>()
                                    .like(SysRoleDao::getOldData, String.format("roleUuid=%s", roleUuid));
                            SysRoleDao sysRoleDao = sysRoleDaoMapper.selectOne(lw);
                            if (sysRoleDao != null) {
                                SysUserRole sysUserRole = new SysUserRole();
                                sysUserRole.setUserId(item.getUserId());
                                sysUserRole.setRoleId(sysRoleDao.getRoleId());
                                sysUserRoleMapper.batchUserRole(Arrays.asList(sysUserRole));
                            }
                        }
                    }
                }
            });
        }
    }
}
